/**
 * 静态分析文件屏蔽
 *
 * Created by nongzhizhong on 2022/08/11.
 */
const chai = require('chai');
chai.use(require('chai-http'));
const common = require('../../common');
const Administrator = require('../../../models/administrator');
const uri = require('../uri');
const benchmark = require('../../../benchmark/static-analyze/gjb.json');
const { shieldFilesStatus } = require('../../../../phoenix/models/files');

module.exports = describe('/PUT /files/shieldFile/:fileId.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  let newUser;

  function url(fileId, status) {
    return `/files/shieldFile/${fileId}.json?status=${status}`;
  }
  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUser = tester;
        return newUser.createRandomProject('c++');
      })
      .then((res) => {
        project = res;
        return project.createStaticAnalyzeVersion(
          benchmark.verified_rules,
          benchmark.files,
          newUser
        );
      })
      .then((res) => {
        projectVersion = res;
      })
      .then(done)
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers([newUser.userId])
      .then(() => {
        done();
      })
      .catch(done);
  });
  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');
  // 文件屏蔽
  it('it should disable of files ', function (done) {
    newUser.login()
      .then(() => {
        chai.request(uri)
          .put(url(projectVersion.rootDirectory.fileId, shieldFilesStatus.disable))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(201);
            projectVersion.rootDirectory.getFile(projectVersion.rootDirectory.fileId)
              .then((file) => {
                file.should.have.property('shieldFilesCount');
                file.shieldFilesCount.should.be.a('object');
                chai.assert.deepEqual(file.shieldFilesCount, { _cpp: 6 });
                done();
              }).catch(done);
          });
      });
  });
  // 取消文件屏蔽
  it('it should enable of files ', function (done) {
    newUser.login()
      .then(() => {
        chai.request(uri)
          .put(url(projectVersion.rootDirectory.fileId, shieldFilesStatus.enable))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(201);
            projectVersion.rootDirectory.getFile(projectVersion.rootDirectory.fileId)
              .then((file) => {
                file.should.have.property('shieldFilesCount');
                file.shieldFilesCount.should.be.a('object');
                chai.assert.deepEqual(file.shieldFilesCount, { _cpp: 0 });
                done();
              }).catch(done);
          });
      });
  });
});
